package com.viknix.threadpool.manager.client.factory;

import com.viknix.threadpool.manager.client.core.ConfigService;
import com.viknix.threadpool.manager.client.core.TpmConfigService;
import com.viknix.threadpool.manager.client.exception.TpmProcessException;
import com.viknix.threadpool.manager.client.http.HttpAgent;

import java.lang.reflect.Constructor;
import java.util.Properties;

/**
 * @Author: Dongqi
 * @Date: 2021/11/24 10:37
 * @Version 1.0
 * @Description:
 */
public class ConfigFactory {
    public ConfigFactory() {
    }

    /**
     * 通过反射构建service
     *
     * @param properties 构造器参数
     * @return {@link TpmConfigService}
     * @throws TpmProcessException
     */
    public static ConfigService createConfigService(Properties properties, HttpAgent httpAgent) throws TpmProcessException {
        try {
            Class<?> driverImplClass = Class.forName("com.viknix.threadpool.manager.client.core.TpmConfigService");
            Constructor constructor = driverImplClass.getConstructor(Properties.class, HttpAgent.class);
            ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties, httpAgent);
            return vendorImpl;
        } catch (Throwable var4) {
            throw new TpmProcessException(-400, var4);
        }
    }

    /**
     * 通过反射构建service
     *
     * @param properties 构造器参数
     * @return {@link TpmConfigService}
     * @throws TpmProcessException
     */
    public static ConfigService createConfigService(Properties properties) throws TpmProcessException {
        try {
            Class<?> driverImplClass = Class.forName("com.viknix.threadpool.manager.client.core.TpmConfigService");
            Constructor constructor = driverImplClass.getConstructor(Properties.class);
            ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties);
            return vendorImpl;
        } catch (Throwable var4) {
            throw new TpmProcessException(-400, var4);
        }
    }

    public static ConfigService createConfigService(String serverAddr) throws TpmProcessException {
        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        return createConfigService(properties);
    }
}
